package com.example.ssmlab2.controller;

import com.example.ssmlab2.model.Book;
import com.example.ssmlab2.model.User;
import com.example.ssmlab2.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("user")
public class UserController extends BaseController{

    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping("signUp")
    private  String signUp(User user){
        if(userService.queryOne("queryUser",user)!=null){
            request.setAttribute("error","Username is already exits");
            return "redirect:/sign_up.jsp";
        }else{
            userService.create(user);
            session.setAttribute("user",user);
            return "redirect:/index.jsp";}


    }
    @RequestMapping("signIn")
    private  String signIn(User user){
        user=userService.queryOne("queryUserByPasswordAndUsername",user);
        if(user==null){
            request.setAttribute("error","Invalid Username or Password");
            return "redirect:/index.jsp";
        }
        session.setAttribute("user",user);
        return "redirect:/admin.jsp";


    }

}